﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FbKPI.Model;
using FbKPI.Facade;


namespace FBKPI.ProcessRequest
{
    /// <summary>
    /// Summary description for MethodDetailHandler
    /// </summary>
    public class MethodDetailHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string MethodName = string.Empty;

            MethodName = context.Request.Params["method"];

            if (MethodName == "GetByMethodID")
            {
                int MethodID = Convert.ToInt32(context.Request.Params["MethodID"].ToString());
                MethodDetailFacade fd = new MethodDetailFacade();
                List<MethodDetail> list = fd.GetByMethodID(MethodID);
                var result = list;
                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(result));

            }

            if (MethodName == "GetByKPIMethod")
            {
                int KpiId = Convert.ToInt32(context.Request.Params["KpiId"].ToString());
                KPIMethodFacade fd = new KPIMethodFacade();
                List<KPIMethod> list = fd.GetListByKpiId(KpiId);
                var result = list;
                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(result));

            }

            if (MethodName == "GetByKPIAchievment")
            {
                int KpiId = Convert.ToInt32(context.Request.Params["KpiId"].ToString());
                KPIMethodFacade fd = new KPIMethodFacade();
                List<KPIMethod> list = fd.GetListByKpiId(KpiId);
                
                var result = list.Where(x=>x.Status==true);
                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(result));

            }

            if (MethodName == "GetByKPIAchievmentEdit")
            {
                int KPIAssignID = Convert.ToInt32(context.Request.Params["KPIAssignID"].ToString());
                KPIAchievementFacade fd = new KPIAchievementFacade();
                List<KPIAchievement> list = fd.GetListKPIAchievement(KPIAssignID);

                var result = list;
                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(result));

            }

            if (MethodName == "GetByKPIAttachment")
            {
                int KpiId = Convert.ToInt32(context.Request.Params["KpiId"].ToString());
                int KPIAssignID = Convert.ToInt32(context.Request.Params["KPIAssignID"].ToString());
                int KPIAssignDetailID = Convert.ToInt32(context.Request.Params["KPIAssignDetailID"].ToString());

                KPIAttachmentFacade fd = new KPIAttachmentFacade();
                IList<KPIAttachment> list = fd.GetKPIAttachmentByKPIAssign_ByKpiId_KpiAssign_KpiAssignDtl(KpiId,KPIAssignID,KPIAssignDetailID);

                var result = list;
                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(result));

            }

            if (MethodName == "GetByKPIAttachmentVerificator")
            {
                //int KpiId = Convert.ToInt32(context.Request.Params["KpiId"].ToString());
                int KPIAssignID = Convert.ToInt32(context.Request.Params["KPIAssignID"].ToString());
                //int KPIAssignDetailID = Convert.ToInt32(context.Request.Params["KPIAssignDetailID"].ToString());

                KPIAttachmentFacade fd = new KPIAttachmentFacade();
                IList<KPIAttachment> list = fd.GetKPIAttachmentByKPIAssign_KpiAssign(KPIAssignID);

                var result = list;
                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(result));

            }

            if (MethodName == "GetKPIAttachmentByKpiId")
            {
                int KpiId = Convert.ToInt32(context.Request.Params["KpiId"].ToString());
                //int KPIAssignID = Convert.ToInt32(context.Request.Params["KPIAssignID"].ToString());
                ////int KPIAssignDetailID = Convert.ToInt32(context.Request.Params["KPIAssignDetailID"].ToString());

                KPIAttachmentFacade fd = new KPIAttachmentFacade();
                IList<KPIAttachment> list = fd.GetKPIAttachment_ByKpiId(KpiId);

                var result = list;
                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(result));

            }

            if (MethodName == "GetByKPIChild")
            {
                int KpiId = Convert.ToInt32(context.Request.Params["KpiId"].ToString());
                //int KPIAssignID = Convert.ToInt32(context.Request.Params["KPIAssignID"].ToString());
                //int KPIAssignDetailID = Convert.ToInt32(context.Request.Params["KPIAssignDetailID"].ToString());

                KPIFacade fd = new KPIFacade();
                KPI objKPI = new KPI();
                objKPI.ID =KpiId;
                IList<KPI> list = fd.GetKPIChilds(objKPI);

                var result = list;
                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(result));

            }

       }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}